package com.senter.demo.uhf.common.proxy;

import com.google.common.base.Preconditions;
import com.senter.support.openapi.StUhf;
import com.senter.support.openapi.StUhf.AccessPassword;
import com.senter.support.openapi.StUhf.Bank;
import com.senter.support.openapi.StUhf.InterrogatorModelA;
import com.senter.support.openapi.StUhf.InterrogatorModelB;
import com.senter.support.openapi.StUhf.InterrogatorModelC;
import com.senter.support.openapi.StUhf.InterrogatorModelE;
import com.senter.support.openapi.StUhf.InterrogatorModelE.UmeInventoryTagInfo;
import com.senter.support.openapi.StUhf.UII;
import com.senter.support.openapi.StUhf.InterrogatorModelE.UmeMask;
import com.senter.support.openapi.StUhf.Result.ReadResult;
import com.senter.support.openapi.StUhf.Result.WriteResult;

public class StUhfProxy
{
	private final StUhf mStUhf;
	public StUhfProxy(StUhf stUhf)
	{
		Preconditions.checkNotNull(stUhf);
		mStUhf=stUhf;
	}
	
	
	public void init()
	{
		if (mStUhf.init()==false)
		{
			throw new IllegalStateException();
		}
	}
	
	public void uninit()
	{
		mStUhf.uninit();
	}
	
	public byte[] readFromAnyTag(AccessPassword apwd,Bank bank,int start,int length)
	{
		switch (mStUhf.getInterrogatorModel())
		{
			case InterrogatorModelA:
			{
				ReadResult rr=mStUhf.getInterrogatorAs(InterrogatorModelA.class).readDataFromSingleTag(apwd, bank, start, length);
				if (rr==null)
				{
					return null;
				}
				
				if (rr.isSucceeded()==false)
				{
					return null;
				}
				
				return rr.getData();
			}
			case InterrogatorModelB:
			{
				ReadResult rr=mStUhf.getInterrogatorAs(InterrogatorModelB.class).readDataFromSingleTag(apwd, bank, start, length);
				if (rr==null)
				{
					return null;
				}
				
				if (rr.isSucceeded()==false)
				{
					return null;
				}
				
				return rr.getData();
			}
			case InterrogatorModelC:
			{
				ReadResult rr=mStUhf.getInterrogatorAs(InterrogatorModelC.class).readDataFromSingleTag(apwd, bank, start, length);
				if (rr==null)
				{
					return null;
				}
				
				if (rr.isSucceeded()==false)
				{
					return null;
				}
				
				return rr.getData();
			}
			case InterrogatorModelD1:
			case InterrogatorModelD2:
			{
				break;
			}
			case InterrogatorModelE:
			{
				return mStUhf.getInterrogatorAs(InterrogatorModelE.class).read(apwd, UmeMask.newMaskAsAny(), bank, start, length);
			}
			default:
				break;
		}
		return null;
	}
	
	
	public boolean writeIntoAnyTag(AccessPassword apwd,Bank bank,int start,byte[] data)
	{
		switch (mStUhf.getInterrogatorModel())
		{
			case InterrogatorModelA:
			{
				WriteResult rr=mStUhf.getInterrogatorAs(InterrogatorModelA.class).writeWordDataToSingleTag(apwd, bank, start, data);
				if (rr==null)
				{
					return false;
				}
				
				if (rr.isSucceeded()==false)
				{
					return false;
				}
				
				return data.length==rr.getWrittenWordsNum()*2;
			}
			case InterrogatorModelB:
			{
				WriteResult rr=mStUhf.getInterrogatorAs(InterrogatorModelB.class).writeWordDataToSingleTag(apwd, bank, start, data);
				if (rr==null)
				{
					return false;
				}
				
				if (rr.isSucceeded()==false)
				{
					return false;
				}
				
				return data.length==rr.getWrittenWordsNum()*2;
			}
			case InterrogatorModelC:
			{
				WriteResult rr=mStUhf.getInterrogatorAs(InterrogatorModelC.class).writeDataToSingleTag(apwd, bank, start, data);
				if (rr==null)
				{
					return false;
				}
				
				if (rr.isSucceeded()==false)
				{
					return false;
				}
				
				return data.length==rr.getWrittenWordsNum()*2;
			}
			case InterrogatorModelD1:
			case InterrogatorModelD2:
			{
				break;
			}
			case InterrogatorModelE:
			{
				Boolean b= mStUhf.getInterrogatorAs(InterrogatorModelE.class).write(apwd, UmeMask.newMaskAsAny(), bank, start, data);
				if (b==null)
				{
					return false;
				}
				return b;
			}
			default:
				break;
		}
		return false;
	}
	
	
	public UII inventoryAnyTag()
	{
		switch (mStUhf.getInterrogatorModel())
		{
			case InterrogatorModelA:
			{
				UII rr=mStUhf.getInterrogatorAs(InterrogatorModelA.class).inventorySingleStep();
				
				return rr;
			}
			case InterrogatorModelB:
			{
				UII rr=mStUhf.getInterrogatorAs(InterrogatorModelB.class).inventorySingleStep();
				
				return rr;
			}
			case InterrogatorModelC:
			{
				UII rr=mStUhf.getInterrogatorAs(InterrogatorModelC.class).inventorySingleStep();
				
				return rr;
			}
			case InterrogatorModelD1:
			case InterrogatorModelD2:
			{
				break;
			}
			case InterrogatorModelE:
			{
				UmeInventoryTagInfo b= mStUhf.getInterrogatorAs(InterrogatorModelE.class).inventorySingleStep(1000);
				if (b==null)
				{
					return null;
				}
				
				return b.getUii();
			}
			default:
				break;
		}
		return null;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
